SitesController.executeUnlink   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 30
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 27
dl 0
loc 30
rs 9.232
c 0
b 0
f 0
1
import Table from 'cli-table'
2
import {existsSync, readdirSync, unlinkSync, writeFileSync} from 'fs'
3
import Nginx from '../services/nginx'
4
import nginxShopware6Template from '../templates/nginx/apps/shopware6'
5
import nginxLaravelTemplate from '../templates/nginx/apps/laravel'
6
import nginxMagento1Template from '../templates/nginx/apps/magento1'
7
import nginxMagento2Template from '../templates/nginx/apps/magento2'
8
import {error, info, success, url} from '../utils/console'
9
import {ensureDirectoryExists} from '../utils/filesystem'
10
import {getConfig, jaleSitesPath} from '../utils/jale'
11
import SecureController from './secureController'
12
import kleur from 'kleur'
13
14
class SitesController {
15
16
    appTypes = ['shopware6', 'laravel', 'magento2', 'magento1']
17
18
    listLinks = async (): Promise<void> => {
19
        const config = await getConfig()
20
        await ensureDirectoryExists(jaleSitesPath)
21
        const sites = readdirSync(jaleSitesPath).map(fileName => fileName.replace(`.${config.tld}.conf`, ''))
22
23
        if (sites.length) {
24
            info(`Currently there ${sites.length > 1 ? 'are' : 'is'} ${sites.length} active Nginx vhost ${sites.length > 1 ? 'configurations' : 'configuration'}\n`)
25
26
            const table = new Table({
27
                head: ['Project', 'Secure'],
28
                colors: false
29
            })
30
31
            for (const site of sites) {
32
                const secure = new SecureController(site).isSecure()
33
                table.push([`${site}.${config.tld}`, (secure ? kleur.green('Yes') : kleur.red('No'))])
34
            }
35
36
            console.log(table.toString())
37
        } else {
38
            info(`Currently there ${sites.length > 1 ? 'are' : 'is'} no active Nginx vhost ${sites.length > 1 ? 'configurations' : 'configuration'}`)
39
        }
40
    }
41
42
    executeLink = async (type: string | undefined, name: string | undefined): Promise<void> => {
43
        const config = await getConfig()
44
        let appType = config.defaultTemplate
45
46
        if (type)
47
            appType = type
48
49
        if (!this.appTypes.includes(appType)) {
50
            error(`Invalid app type ${appType}. Please select one of: ${this.appTypes.join(', ')}`)
51
            return
52
        }
53
54
        const project = process.cwd().substring(process.cwd().lastIndexOf('/') + 1)
55
        const domain = name || project
56
        const hostname = `${domain}.${config.tld}`
57
58
        info(`Linking ${project} to ${hostname}...`)
59
60
        await ensureDirectoryExists(jaleSitesPath)
61
62
        this.createNginxConfig(appType, hostname, project)
63
64
        await (new Nginx()).reload()
65
66
        success(`Successfully linked ${domain}. Access it from ${url(`http://${hostname}`)}.`)
67
    }
68
69
    executeUnlink = async (): Promise<void> => {
70
        const config = await getConfig()
71
72
        const project = process.cwd().substring(process.cwd().lastIndexOf('/') + 1)
73
74
        let filename = `${project}.${config.tld}.conf`
75
76
        readdirSync(jaleSitesPath).forEach(file => {
77
            if (file.includes(project)) {
78
                filename = file
79
            }
80
        })
81
82
        if (!existsSync(`${jaleSitesPath}/${filename}`)) {
83
            error(`This project doesn't seem to be linked because the configuration file can't be found: ${jaleSitesPath}/${filename}`)
84
            return
85
        }
86
87
        info(`Unlinking ${project}...`)
88
89
        const secureController = new SecureController
90
91
        if (existsSync(secureController.crtPath))
92
            await secureController.executeUnsecure()
93
94
        unlinkSync(`${jaleSitesPath}/${filename}`)
95
96
        await (new Nginx()).reload()
97
98
        success(`Successfully unlinked ${project}.`)
99
    }
100
101
    /**
102
     * Create a Nginx template for the provided hostname with a specific template.
103
     *
104
     * @param appType
105
     * @param hostname
106
     * @param project
107
     */
108
    createNginxConfig = (appType: string, hostname: string, project: string): void => {
109
        switch (appType) {
110
        case 'shopware6':
111
            writeFileSync(`${jaleSitesPath}/${project}.conf`, nginxShopware6Template(hostname, process.cwd()))
112
            break
113
        case 'magento2':
114
            writeFileSync(`${jaleSitesPath}/${project}.conf`, nginxMagento2Template(hostname, process.cwd()))
115
            break
116
        case 'magento1':
117
            writeFileSync(`${jaleSitesPath}/${project}.conf`, nginxMagento1Template(hostname, process.cwd()))
118
            break
119
        default:
120
            writeFileSync(`${jaleSitesPath}/${project}.conf`, nginxLaravelTemplate(hostname, process.cwd()))
121
            break
122
        }
123
    }
124
125
126
}
127
128
export default SitesController